from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Topic(models.Model):
    '''用户学习的主题'''
    text = models.CharField(max_length= 200)
    date_added = models.DateTimeField(auto_now_add=True)  # 放置时间戳
    # 只需将最高层数据Topic绑定相应的用户，底层数据自动关联用户
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        '''返回模型的字符串表示'''
        return self.text

class Entry(models.Model):
    '''与主题条目关联的知识'''

    # 外键：建立数据联系，并设置级联删除
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'entries'

    def __str__(self):
        '''返回模型的字符串表示'''
        return f'{self.text[:50]}...'  # 返回前50个字符